/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.EmployeeService;
import com.lmx.persistence.service.NursingService;
import com.lmx.persistence.service.WorkTimeSettingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.WorkTimeSetting;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(WorkTimeSetting.class)
@Component
public class WorkTimeSettingRepository extends BaseRepository<WorkTimeSetting> {

	@Autowired
	private NursingService nursingService;
	@Autowired
	private WorkTimeSettingService service;
	@Autowired
	private EmployeeService employeeService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(WorkTimeSetting.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<WorkTimeSetting> findAll(final QueryParams requestParams) {
		Map<String, String> filter = CommonUtil
				.getKeyMapByFilter(requestParams);
		if (filter.containsKey("forTenant")
				&& CommonConstants.DEFAULT_STATUS_YES_1.toString().equals(
						filter.get("forTenant"))) {
			Long tenantId = ActionContextHolder.getContext().getTenantId();
			return this.service.findNotDelete("tenant.id", tenantId);
		}
		return super.findAll(requestParams);
	}

	@Override
	@JsonApiSave
	public WorkTimeSetting save(WorkTimeSetting setting) {
		if (setting.getId() == null) {
			List<WorkTimeSetting> temp = nursingService
					.getWorkTimeSettingByName(setting.getName());
			if (temp == null || temp.size() == 0) {
				//dslJpaService.create(setting);
				int ktFlag=employeeService.WorkTimeSettingKTFlag(setting);
				setting.setKtFlag(ktFlag);
				super.save(setting);
			} else {
				setting.setErrcode(4);// name
				throw new BusinessException(null,
						CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}
		} else {
			if (setting.getDelStatus() == 1) {// 删除
				dslJpaService.update(setting);
			} else {//
				int updateFlag = 1;
				WorkTimeSetting oldData = nursingService
						.getWorkTimeSettingById(setting.getId());
				if (oldData.getName().equals(setting.getName())) {

				} else {
					List<WorkTimeSetting> temp = nursingService
							.getWorkTimeSettingByName(setting.getName());
					if (temp == null || temp.size() == 0) {

					} else {
						updateFlag = 0;
					}
				}

				if (updateFlag == 1) {
					//dslJpaService.update(setting);
					int ktFlag=employeeService.WorkTimeSettingKTFlag(setting);
					setting.setKtFlag(ktFlag);
					super.save(setting);
				} else {
					setting.setErrcode(4);// name重复
					throw new BusinessException(null,
							CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
			}
		}
		return setting;
	}

}
